package com.fq.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.fq.myapplication.fragment.CommentFragment;

import com.fq.myapplication.R;
import com.fq.myapplication.bean.Message;

import java.util.List;

public class MessageAdapter extends BaseAdapter {
    private Context context;
    private List<Message> dataList;
    private static final int IMAGE_RESOURCE_1 = R.drawable.star_border;
    private static final int IMAGE_RESOURCE_2 = R.drawable.star;
    private static final String MESSAGE_COLLECT_SUCCESS = "收藏成功！";
    private static final String MESSAGE_CANCEL_COLLECT = "取消收藏";

    public MessageAdapter(Context context, List<Message> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_home_message_item, parent, false);
            holder = new ViewHolder();

            // 初始化视图组件
            holder.ivAvatar = convertView.findViewById(R.id.iv_avatar);
            holder.tvUsername = convertView.findViewById(R.id.tv_username);
            holder.tvTime = convertView.findViewById(R.id.tv_release_time);
//            holder.ivContent = convertView.findViewById(R.id.iv_content);
            holder.gvImages = convertView.findViewById(R.id.gv_images);
            holder.tvLike = convertView.findViewById(R.id.tv_like);
            holder.tvComment = convertView.findViewById(R.id.tv_comment);
            holder.likeCountText = convertView.findViewById(R.id.like_count_text);
            holder.tvShare = convertView.findViewById(R.id.tv_share);
            holder.ivCollect = convertView.findViewById(R.id.iv_collect); // 假设布局中有收藏图标
            // holder.tvCollect = convertView.findViewById(R.id.tv_collect);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 绑定数据
        Message post = dataList.get(position);
        holder.ivAvatar.setImageResource(post.getAvatarRes());
        holder.tvUsername.setText(post.getUsername());
        holder.tvTime.setText(post.getTime());
//        holder.ivContent.setImageResource(post.getContentImageRes());
        holder.tvLike.setText(String.valueOf(post.getLikeCount()));
        holder.tvComment.setText(String.valueOf(post.getCommentCount()));
        // holder.tvCollect.setText(post.getCollect());
        boolean isCollected = post.isCollected();
        holder.ivCollect.setImageResource(isCollected ? IMAGE_RESOURCE_2 : IMAGE_RESOURCE_1);


        //九宫格图片
        List<Integer> imageList = post.getImageList();
        if(imageList != null && !imageList.isEmpty()){
            ImageAdapter imageAdapter = new ImageAdapter(context,imageList);
            holder.gvImages.setAdapter(imageAdapter);
        }
        // 分享
        holder.tvShare.setOnClickListener(v -> {
            // 执行分享操作
            showClickableToast(post);
        });
        // 评论
        holder.tvComment.setOnClickListener(v -> {
            CommentFragment commentFragment = new CommentFragment();
            commentFragment.show(((FragmentActivity) context).getSupportFragmentManager(), commentFragment.getTag());
        });
        // 点赞
        holder.tvLike.setOnClickListener(v -> {
            showLikeAnimation(holder.likeCountText);
            post.setLikeCount(post.getLikeCount() + 1);
            holder.tvLike.setText(String.valueOf(post.getLikeCount()));
        });
        // 收藏点击事件
        holder.ivCollect.setOnClickListener(v -> {
            post.setCollected(!post.isCollected());
            holder.ivCollect.setImageResource(post.isCollected() ? IMAGE_RESOURCE_2 : IMAGE_RESOURCE_1);
            showResultDialog(post.isCollected());
        });

        return convertView;
    }

    // ViewHolder 内部类
    private static class ViewHolder {
        ImageView ivAvatar;
        TextView tvUsername;
        TextView tvTime;
//        ImageView ivContent;
        GridView gvImages;
        TextView tvLike;
        TextView tvComment;
        TextView tvShare;
        TextView likeCountText;
        ImageView ivCollect; // 新增收藏图标
//        TextView tvCollect;//收藏数据
    }

    //
    private void showClickableToast(Message post) {
        // ✅ 正确获取 LayoutInflater（通过 Adapter 的 Context）
        LayoutInflater inflater = LayoutInflater.from(context);
        View toastView = inflater.inflate(R.layout.dialog_share, null);

        // ✅ 正确获取 Toast 的 Context
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(toastView);
        toast.show();
    }

    private void showLikeAnimation(TextView likeCountText) {
        likeCountText.setVisibility(View.VISIBLE);

        // 创建透明度动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
        alphaAnimation.setDuration(1000);

        // 创建位移动画
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, -1f);
        translateAnimation.setDuration(1000);

        // 创建动画集合
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(translateAnimation);

        // 设置动画监听器
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                likeCountText.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        // 开始动画
        likeCountText.startAnimation(animationSet);
    }

    private void showResultDialog(boolean isCollected) {
        String message = isCollected ? MESSAGE_COLLECT_SUCCESS : MESSAGE_CANCEL_COLLECT;
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }



    // 图片适配器
    private static class ImageAdapter extends BaseAdapter {
        private Context context;
        private List<Integer> imageList;

        public ImageAdapter(Context context, List<Integer> imageList) {
            this.context = context;
            this.imageList = imageList;
        }

        @Override
        public int getCount() {
            return Math.min(imageList.size(), 9);
        }

        @Override
        public Object getItem(int position) {
            return imageList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 200));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(imageList.get(position));
            return imageView;
        }
    }
}


